<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>foreach</title>
</head>

<body>

    <script>
        // foreach----------------------------------------------------
        Array.prototype.forEachCustom = function (fn, thisV) {
            try {
                for (let i in this) {
                    fn(this[i], i, this); 
                }
            } catch (e) {
                if (e === forEachCustom.break) return;
                else throw e;
            }
        }
        let arrf = [22, 33, 44, 55]
        let arrf2 = arrf.forEachCustom(function (val, i) {
            console.log("下标" + i + ":" + val);
        })



        // 作业修改--------------------------------------
        //深拷贝---不能用typeof--------------------------------------------------
        // 备注： JSON.parse(JSON.stringify(obj1));  //无效function
        // function deepClone(data) {
        //     var type =Object.prototype.toString.call(data);
        //     var obj;
        //     if (type === '[object'+'Array]') {
        //         obj = [];
        //     } else if (type ===  '[object'+'Object]') {
        //         obj = {};
        //     } else {
        //         return data;
        //     }
        //     if (type === 'array') {
        //         for (let i = 0; i < data.length; i++) {
        //             obj.push(data[i]);
        //         }
        //     } else if (type === 'object') {
        //         obj = JSON.parse(JSON.stringify(data));
        //     }
        //     return obj;
        // }
        function deepClone(data) {
            var type =Object.prototype.toString.call(data);
            var obj;
           
            // object 
            if (type.slice(8,-1)==="Array") {
                obj = [];
                for (let i = 0; i < data.length; i++) {
                    obj.push(data[i]);
                }
            } else if (type.slice(8,-1)==="Object") {
                obj = {};
                obj = JSON.parse(JSON.stringify(data));
            } else {
                return data;
            }
            return obj;
        }
        // 测试
        let arr = [2, 3, 4, 5];
        let arr2 = deepClone(arr);
        console.log(arr2); //[2,3,4,5]
        arr2[0] = 1000;
        console.log(arr); //[2,3,4,5]
        console.log(arr2); //[1000, 3, 4, 5]

        function a() {
            return 23;
        }
        // function b(){};
        let b = deepClone(a);
        console.log(b);

        // 防抖--------------
        function debunce(handler, delay) {
            //handler是要传入的进行防抖的函数，delay是等待时间。
            var timer = null;
            // console.log("timer = null");
            return function() {
                clearTimeout(timer); //每次都要清除这个定时器 
                timer = setTimeout(
                    function() { //重新开启定时器 
                        handler();
                    },
                    delay);
            }
        }
        // 当用户滚动时被调用的函数
        function foo() {
            console.log('success!');
        }
        window.addEventListener('scroll', debunce(foo, 500));

        // 节流---------------------
        function throttle(handler, wait) { //handler是要进行节流的函数，wait是等待时间
            var lastTime = 0;
            return function() {
                var nowTime = new Date().getTime(); //获取当前时间
                if (nowTime - lastTime > wait) {
                    handler();
                    lastTime = nowTime; //更新最后时间
                }
            }
        }

        function handler() {
            console.log("throttle");
        };
        window.addEventListener("scroll", throttle(handler, 1000))
    </script>
</body>

</html>